package com.woniu.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.account.dao.TAdminRoleMapper;
import com.woniu.account.dao.TRoleMapper;
import com.woniu.account.model.TAdminRole;
import com.woniu.account.model.TRole;
import com.woniu.account.service.TRoleService;
import com.woniu.core.util.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年04月02日
 */
@Service
public class TRoleServiceImpl extends ServiceImpl<TRoleMapper, TRole> implements TRoleService {

    @Resource
    private TRoleMapper tRoleMapper;
    @Resource
    private TAdminRoleMapper tAdminRoleMapper;
    @Override
    public PageInfo<TRole> search(Integer pageNum, Integer pageSize) throws Exception {
        Page<TRole> page = new Page<>(pageNum==null?1:pageNum,pageSize==null?10:pageSize);
        QueryWrapper<TRole> wrapper = new QueryWrapper<TRole>();
        tRoleMapper.selectPage(page,wrapper);
        List<TRole> list = page.getRecords();
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setList(list);
        pageInfo.setPages(page.getPages());
        return pageInfo;
    }

    @Override
    public TRole insert(String roleName) throws Exception {
        QueryWrapper<TRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_name",roleName);
        TRole role = tRoleMapper.selectOne(wrapper);
        if(role!=null){
            throw  new Exception("角色已存在，不能新增");
        }
        //没有的话，新增要new一个
        TRole role1 = new TRole();
        role1.setRoleName(roleName);
        tRoleMapper.insert(role1);
        return role1;
    }

    @Override
    public TRole delById(Integer roleId) throws Exception {
        TRole role = tRoleMapper.selectById(roleId);
        if(role==null){
            throw new Exception("角色不存在，无法删除");
        }
        QueryWrapper<TAdminRole> accountRole = new QueryWrapper<>();
        accountRole.eq("role_id",roleId);
        List<TAdminRole> list = tAdminRoleMapper.selectList(accountRole);
        if(list.size()>0){
            throw new Exception("该角色已被使用,不能删除");
        }
        QueryWrapper<TRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        tRoleMapper.delete(wrapper);
        return role;
    }

    @Override
    public TRole updateName(Integer roleId, String roleName) throws Exception {
        TRole role = tRoleMapper.selectById(roleId);
        if(role==null){
            throw new Exception("角色不存在，不能修改");
        }
        if(role.getRoleName().equals(roleName)){
            throw new Exception("角色名称重复");
        }
        UpdateWrapper<TRole> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("role_id",roleId);
        updateWrapper.set("role_name",roleName);
        tRoleMapper.update(null,updateWrapper);
        return role;
    }

    @Override
    public List<TRole> findAllRole() throws Exception {
        QueryWrapper<TRole> wrapper = new QueryWrapper<TRole>();
        wrapper.select("role_id","role_name");
        List<TRole> roles = tRoleMapper.selectList(wrapper);

        return roles;
    }
}
